################################################################################
##
## Purpose: This script creates Figure 4, along with SI table 4
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/prepped/finalData.RData: Prepped data from 9_DATA_final_build.R
##
## Output Files:
##  - ./output/figures/MS_figure_4.pdf
##  - ./output/tables/SI_table_4.tex
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
##
################################################################################
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)
require(fixest)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()


load('./data/prepped/finalData.RData')
dims <- colnames(utterance_level %>% select(matches('SENT_'),-matches('_lag|error')) %>% select(-matches('SEVERE|AUTHOR|LIKELY')))

# Speaker intercepts, controlling for document, with minimum utterances > 100
modSenateInted <- feols(as.formula(paste0('interrupted ~ factor(opensecretsID)',
                                ' + poly(scale(log(nchars)),3) + interruptor +', # U-level covariates
                                paste(paste0('topic_',1:100),collapse = ' + '),
                                ' + ',
                                paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                ' + poly(scale(log(tot_utterances_lag)),3) + scale(votepct_lag) + gender_lag + scale(seniority_lag)', # X-level covariates
                                ' + scale(age_lag) + scale(nominate_dim1_lag) + constrain_empower_tot_lag + yellen_vote_lag',
                                '| docID')),
              utterance_level %>% 
                filter(all > 30,
                       !grepl('EXPERT',opensecretsID),
                       chamber == 'Senate',
                       ind > mind),
              cluster = 'interruptedBy')

toplotChamber <- modSenateInted$coeftable %>%
  data.frame() %>%
  rename_all(function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
  mutate(covs =rownames(.)) %>%
  filter(grepl('FED',covs)) %>%
  mutate(covs =  gsub('factor\\(opensecretsID\\)','',covs)) %>%
  as_tibble() %>%
  left_join(utterance_level %>% select(opensecretsID,gender,name) %>% distinct(),
            by = c('covs' = 'opensecretsID')) %>%
  mutate(stab = ifelse(grepl('FED',covs),'DC',stab),
         id = paste0(str_to_title(name),' (',gender,')'),
         fedID = ifelse(grepl('FED',covs),'Fed Chair','Not'))

modHouseInted <- feols(as.formula(paste0('interrupted ~ factor(opensecretsID)',
                                          ' + poly(scale(log(nchars)),3) + interruptor +', # U-level covariates
                                          paste(paste0('topic_',1:100),collapse = ' + '),
                                          ' + ',
                                          paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                          ' + poly(scale(log(tot_utterances_lag)),3) + scale(votepct_lag) + gender_lag + scale(seniority_lag)', # X-level covariates
                                          ' + scale(age_lag) + scale(nominate_dim1_lag) + constrain_empower_tot_lag + yellen_vote_lag',
                                          '| docID')),
                        utterance_level %>% 
                          filter(all > 30,
                                 !grepl('EXPERT',opensecretsID),
                                 chamber == 'House',
                                 ind > mind),
                        cluster = 'interruptedBy')

toplotChamber <- toplotChamber %>%
  mutate(chamber = 'Senate') %>%
  bind_rows(modHouseInted$coeftable %>%
              data.frame() %>%
              rename_all(function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
              mutate(covs =rownames(.)) %>%
              filter(grepl('FED',covs)) %>%
              mutate(covs =  gsub('factor\\(opensecretsID\\)','',covs)) %>%
              as_tibble() %>%
              left_join(utterance_level %>% select(opensecretsID,gender,name) %>% distinct(),
                        by = c('covs' = 'opensecretsID')) %>%
              mutate(stab = ifelse(grepl('FED',covs),'DC',stab),
                     id = paste0(str_to_title(name),' (',gender,')'),
                     fedID = ifelse(grepl('FED',covs),'Fed Chair','Not'),
                     chamber = 'House'))




# Interruptors
modSenateIntor <- feols(as.formula(paste0('interruptor ~ factor(opensecretsID)',
                                         ' + poly(scale(log(nchars)),3) + interrupted +', # U-level covariates
                                         paste(paste0('topic_',1:100),collapse = ' + '),
                                         ' + ',
                                         paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                         ' + poly(scale(log(tot_utterances_lag)),3) + scale(votepct_lag) + gender_lag + scale(seniority_lag)', # X-level covariates
                                         ' + scale(age_lag) + scale(nominate_dim1_lag) + constrain_empower_tot_lag + yellen_vote_lag',
                                         '| docID')),
                       utterance_level %>% 
                         filter(all > 30,
                                !grepl('EXPERT',opensecretsID),
                                chamber == 'Senate',
                                ind > mind),
                       cluster = 'opensecretsID')

modHouseIntor <- feols(as.formula(paste0('interruptor ~ factor(opensecretsID)',
                                         ' + poly(scale(log(nchars)),3) + interrupted +', # U-level covariates
                                         paste(paste0('topic_',1:100),collapse = ' + '),
                                         ' + ',
                                         paste(paste0('scale(',dims[-which(grepl('comb',dims))],'_lag)'),collapse = ' + '),
                                         ' + poly(scale(log(tot_utterances_lag)),3) + scale(votepct_lag) + gender_lag + scale(seniority_lag)', # X-level covariates
                                         ' + scale(age_lag) + scale(nominate_dim1_lag) + constrain_empower_tot_lag + yellen_vote_lag',
                                         '| docID')),
                       utterance_level %>% 
                         filter(all > 30,
                                !grepl('EXPERT',opensecretsID),
                                chamber == 'House',
                                ind > mind),
                       cluster = 'opensecretsID')

toplotChamber <- toplotChamber %>%
  mutate(outcome = 'Interrupted') %>%
  bind_rows(modSenateIntor$coeftable %>%
              data.frame() %>%
              rename_all(function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
              mutate(covs =rownames(.)) %>%
              filter(grepl('FED',covs)) %>%
              mutate(covs =  gsub('factor\\(opensecretsID\\)','',covs)) %>%
              as_tibble() %>%
              left_join(utterance_level %>% select(opensecretsID,gender,name) %>% distinct(),
                        by = c('covs' = 'opensecretsID')) %>%
              mutate(stab = ifelse(grepl('FED',covs),'DC',stab),
                     id = paste0(str_to_title(name),' (',gender,')'),
                     fedID = ifelse(grepl('FED',covs),'Fed Chair','Not')) %>%
              mutate(outcome = 'Interruptor',
                     chamber = 'Senate'))


toplotChamber <- toplotChamber %>%
  bind_rows(modHouseIntor$coeftable %>%
              data.frame() %>%
              rename_all(function(x) gsub('Estimate','est',gsub('Std..Error','se',gsub('t.value','tstat',gsub('Pr...t..','pval',x))))) %>%
              mutate(covs =rownames(.)) %>%
              filter(grepl('FED',covs)) %>%
              mutate(covs =  gsub('factor\\(opensecretsID\\)','',covs)) %>%
              as_tibble() %>%
              left_join(utterance_level %>% select(opensecretsID,gender,name) %>% distinct(),
                        by = c('covs' = 'opensecretsID')) %>%
              mutate(stab = ifelse(grepl('FED',covs),'DC',stab),
                     id = paste0(str_to_title(name),' (',gender,')'),
                     fedID = ifelse(grepl('FED',covs),'Fed Chair','Not')) %>%
              mutate(outcome = 'Interruptor',
                     chamber = 'House'))


pdf('./output/figures/MS_figure_4.pdf',width = 7,height = 4)
toplotChamber %>%
  mutate(id = factor(id,levels = rev(c('Alan Greenspan (M)',
                                       'Janet L. Yellen (F)',
                                       'Jerome H. Powell (M)')))) %>%
  # select(all) %>% distinct()
  ggplot(aes(x = est,y = id,color = chamber)) + 
  geom_vline(xintercept = 0,linetype = 'dashed') + 
  geom_errorbarh(aes(xmin = est-2*se,xmax = est+2*se),height = 0,position = position_dodge(width = .25),show.legend = F) + 
  geom_point(aes(shape = chamber),size =3, fill = 'white',position = position_dodge(width = .25)) + 
  theme_ridges() + 
  scale_color_manual(name = 'Chamber',values = c('black','grey50')) +
  # scale_size_continuous(name = 'Total Utterances',range = c(2,5)) + 
  scale_shape_manual(name = 'Chamber',values = c(21,22)) + 
  xlab('Fixed Effect Intercept (Reference = Bernanke)') + 
  ylab('') + 
  guides(shape = guide_legend(override.aes = list(size = 5))) + 
  facet_grid(~outcome) + 
  theme(legend.position = 'bottom',
        strip.background = element_rect(fill = 'white',color = 'black',size = .5,linetype = 'solid'))
dev.off()

etable(modHouseInted,modSenateInted,
       modHouseIntor,modSenateIntor,
       extralines = list('100 LDA Topic Loadings' = c('Yes','Yes')),
       drop = 'opensecretsID\\)N|topic',replace = T,headers = c('House','Senate','House','Senate'),
       file = './output/tables/SI_table_4.tex')

# EOF